package com.jubaozan.service.platform.decorator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jubaozan.c3.framework.utils.ObjectUtil;
import com.jubaozan.c3.framework.utils.PageHolder;
import com.jubaozan.c3.framework.utils.ResultHolder;
import com.jubaozan.dao.youme.domain.Activity;
import com.jubaozan.dao.youme.domain.ActivityGoods;
import com.jubaozan.dao.youme.domain.ActivityLog;
import com.jubaozan.dao.youme.service.ActivityGoodsService;
import com.jubaozan.dao.youme.service.ActivityLogService;
import com.jubaozan.dao.youme.service.ActivityService;
import com.jubaozan.feign.goods.vo.StoreGoodsVO;
import com.jubaozan.feign.user.UserFeignService;
import com.jubaozan.feign.goods.StoreGoodsFeignService;
import com.jubaozan.feign.goods.vo.GoodsVO;
import com.jubaozan.feign.platform.activity.vo.ActivityLogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @ClassName ActivityLogDecorator
 * @Description 活动日志Decorator
 * @Author TanRuixiang
 * @Date 2020/9/24 10:42
 * @Version 1.0
 **/
@Slf4j
@Component
public class PlatformActivityLogDecorator {

    private final ActivityLogService activityLogService;


    private final ActivityGoodsService activityGoodsService;

    private final StoreGoodsFeignService storeGoodsFeignService;


    private final ActivityService activityService;

    @Autowired
    public PlatformActivityLogDecorator(ActivityLogService activityLogService, ActivityGoodsService activityGoodsService,
                                        StoreGoodsFeignService storeGoodsFeignService, ActivityService activityService) {
        this.activityLogService = activityLogService;
        this.activityGoodsService = activityGoodsService;
        this.storeGoodsFeignService = storeGoodsFeignService;
        this.activityService = activityService;
    }

    /**
     * @param storeId: 门店ID
     * @return
     * @Author TanRuixiang
     * @Description 根据平台id获取活动信息
     * @Date 2020年09月24日 15:01
     **/
    public ActivityLogVO getActivityByStoreId(Long storeId) {
        //根据门店ID查询正在进行的活动
        Activity activity = getActivityByTime(storeId, LocalDateTime.now());
        if (ObjectUtil.isEmpty(activity)) {
            return null;
        }
        ActivityLogVO log = new ActivityLogVO();
        QueryWrapper<ActivityLog> qw = new QueryWrapper<>();
        qw.lambda().eq(ActivityLog::getStoreId, storeId).eq(ActivityLog::getActivityId, activity.getId());
        int browseNum = activityLogService.count(qw);
        log.setBrowseNum(Long.valueOf(String.valueOf(browseNum)));
        log.setId(activity.getId());
        log.setEndTime(activity.getEndTime());
        return log;
    }


    /**
     * @param storeId:
     * @param pageHolder:
     * @return
     * @Author TanRuixiang
     * @Description 根据平台id活动下的商品列表
     * @Date 2020年09月25日 19:05
     **/
    public IPage<StoreGoodsVO> getGoodsListByStoreId(Long storeId,Long userId, PageHolder pageHolder) {
        //根据门店ID查询正在进行的活动
        Activity activity = getActivityByTime(storeId, LocalDateTime.now());
        if (ObjectUtil.isEmpty(activity)) {
            return null;
        }
        //分页查询当前活动关联的商品ID
        Page<ActivityGoods> page = new Page<>(pageHolder.getPageNo(), pageHolder.getPageSize());
        QueryWrapper<ActivityGoods> qw = new QueryWrapper<>();
        qw.lambda().eq(ActivityGoods::getActivityId, activity.getId());
        IPage<ActivityGoods> agPage = activityGoodsService.page(page, qw);
        List<ActivityGoods> list = agPage.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<Long> goodsIds = new ArrayList<>();
        list.forEach(item -> {
            goodsIds.add(item.getGoodsId());
        });
        List<StoreGoodsVO> records = getGoodsListByIds(goodsIds, storeId,userId);
        IPage<StoreGoodsVO> iPage = new Page<>();
        iPage.setCurrent(agPage.getCurrent());
        iPage.setPages(agPage.getPages());
        iPage.setSize(agPage.getSize());
        iPage.setTotal(agPage.getTotal());
        iPage.setRecords(records);
        return iPage;
    }


    /**
     * @param goodsIds: 商品ID集合
     * @return
     * @Author TanRuixiang
     * @Description 通过商品ID集合查询到商品信息集合
     * @Date 2020年09月25日 16:46
     **/
    public List<StoreGoodsVO> getGoodsListByIds(List<Long> goodsIds, Long storeId,Long userId) {
        ResultHolder<List<StoreGoodsVO>> holder = storeGoodsFeignService.listGoodsByIds(goodsIds, storeId,userId);
        if (holder.isSuccess()) {
            return holder.getBody();
        }
        return Collections.emptyList();
    }

    /**
     * @param storeId: 平台ID
     * @param dateTime:    时间
     * @return
     * @Author TanRuixiang
     * @Description 根据时间获取这个时间区间是否有活动
     * @Date 2020年09月29日 9:31
     **/
    public Activity getActivityByTime(Long storeId, LocalDateTime dateTime) {
        QueryWrapper<Activity> qw = new QueryWrapper<>();
        qw.lambda().eq(Activity::getStoreId, storeId).le(Activity::getStartTime, dateTime).ge(Activity::getEndTime, dateTime);
        return activityService.getOne(qw);
    }
}
